
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if (!Schema::hasTable('users')) {//如果不存在则建表
            Schema::create('users', function (Blueprint $table) {
                $this->create_table($table);
            });
        } else {//否则增量添加字段
            Schema::table('users', function (Blueprint $table) {
                $this->create_table($table);
            });
        }
        DB::statement("alter table `users` comment'用户表'");
    }
    // 表结构
    public function create_table($table)
    {
        $table->engine = 'InnoDB';
        if (!Schema::hasColumn('users', 'id')) {
            $table->increments('id');
        }
        if (!Schema::hasColumn('users', 'name')) {
            $table->string('name', 32)->default('')->comment('姓名');
        }

        if (!Schema::hasColumn('users', 'group_id')) {
            $table->tinyInteger('group_id')->default(0)->comment('用户组id');
        }
        if (!Schema::hasColumn('users', 'email')) {
            $table->string('email')->comment('邮箱');
        }
        if (!Schema::hasColumn('users', 'password')) {
            $table->string('password', 255)->default('')->comment('密码');
        }
        if (!Schema::hasColumn('users', 'remember_token')) {
            $table->rememberToken();
        }
        if (!Schema::hasColumn('users', 'created_at')) {
            $table->timestamps();
        }
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        // Schema::dropIfExists('users');
    }
}
